//
//  LoginViewController.m
//  音悦咖
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "LoginViewController.h"
#import "UMSocial.h"
#import "D_DataBase.h"
@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *backImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    backImg.image = [UIImage imageNamed:@"img17.jpg"];
    backImg.alpha = 0.8;
    [self.view addSubview:backImg];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT * 0.3)];
    view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:view];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 0.338, SCREEN_HEIGHT * 0.3 * 0.25, SCREEN_HEIGHT * 0.3 / 2, SCREEN_HEIGHT * 0.3 / 2)];
    imgView.image = [UIImage imageNamed:@"lloo"];
    [view addSubview:imgView];
    
 
//--------------------------------------------------------------------------
    UIButton *weixinBut = [UIButton buttonWithType:UIButtonTypeSystem];
    weixinBut.frame = CGRectMake(80, SCREEN_HEIGHT * 0.42, SCREEN_WIDTH - 160, 30);
    weixinBut.layer.backgroundColor = [UIColor clearColor].CGColor;
    weixinBut.layer.borderColor = [UIColor grayColor].CGColor;
    [weixinBut addTarget:self action:@selector(qqLoginAction:) forControlEvents:UIControlEventTouchUpInside];
    weixinBut.layer.borderWidth = 2;
    [weixinBut setTitle:@"新浪微博登录" forState:UIControlStateNormal];
    [self.view addSubview:weixinBut];
    
    
    UIButton *weiboBut = [UIButton buttonWithType:UIButtonTypeSystem];
    weiboBut.frame = CGRectMake(80, SCREEN_HEIGHT * 0.51, SCREEN_WIDTH - 160, 30);
    weiboBut.layer.backgroundColor = [UIColor clearColor].CGColor;
    weiboBut.layer.borderColor = [UIColor grayColor].CGColor;
    weiboBut.layer.borderWidth = 2;
    [weiboBut addTarget:self action:@selector(weiboAction:) forControlEvents:UIControlEventTouchUpInside];
    [weiboBut setTitle:@"腾讯微博分享心情" forState:UIControlStateNormal];
    [self.view addSubview:weiboBut];
    
    UIButton *boboBut = [UIButton buttonWithType:UIButtonTypeSystem];
    boboBut.frame = CGRectMake(80, SCREEN_HEIGHT * 0.615, SCREEN_WIDTH - 160, 30);
    boboBut.layer.backgroundColor = [UIColor clearColor].CGColor;
    boboBut.layer.borderColor = [UIColor grayColor].CGColor;
    boboBut.layer.borderWidth = 2;
    [boboBut setTitle:@"新浪微博分享心情" forState:UIControlStateNormal];
    [boboBut addTarget:self action:@selector(boboAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:boboBut];
    
//    UIButton *registBut = [UIButton buttonWithType:UIButtonTypeSystem];
//    registBut.frame = CGRectMake(80, 450, SCREEN_WIDTH - 160, 30);
//    registBut.layer.backgroundColor = [UIColor clearColor].CGColor;
//    registBut.layer.borderColor = [UIColor grayColor].CGColor;
//    registBut.layer.borderWidth = 2;
//    [registBut setTitle:@"注册啵啵号" forState:UIControlStateNormal];
//    [registBut addTarget:self action:@selector(registButAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:registBut];
    
    
}

// 新浪 登陆
- (void)qqLoginAction:(UIButton *)button
{
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            if ([self.str isEqualToString:@"0"]) {
                // 通知中心
                NSString *name = snsAccount.userName;
                NSString *url = snsAccount.iconURL;
                [[NSNotificationCenter defaultCenter] postNotificationName:@"value" object:nil userInfo:@{@"name":name,@"url":url}];
                  [self.navigationController popToRootViewControllerAnimated:YES];
               
            }else{
                // 传值用户名 图片
                self.bbolock(snsAccount.userName,snsAccount.iconURL);
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
 
            
        }});
    
    //获取accestoken以及新浪用户信息，得到的数据在回调Block对象形参respone的data属性
    [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina  completion:^(UMSocialResponseEntity *response){
        NSLog(@"SnsInformation is %@",response.data);
    }];
    
}

// 腾讯
- (void)weiboAction:(UIButton *)button
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55aa363567e58ebf5300291b"
                                      shareText:@"请输入要分享的文字....."
                                     shareImage:[UIImage imageNamed:@"lloo"]
                                shareToSnsNames:@[UMShareToTencent]
                                       delegate:self];
  }

//实现回调方法（可选）：
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        //得到分享到的微博平台名
        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
    }
}


// 新浪微博分享
- (void)boboAction:(UIButton *)button
{
    [UMSocialSnsService presentSnsIconSheetView:self appKey:SDK_KEY shareText:@"请输入要分享的文字......." shareImage:[UIImage imageNamed:@"headimg"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,nil] delegate:nil];
    
//    [UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToQQ,UMShareToQzone,UMShareToWechatTimeline]];

    
}

// 注册啵啵号按钮
- (void)registButAction:(UIButton *)button
{
//    D_DataBase *data = [[D_DataBase alloc] init];
//    [data openDB];
//    [data deleteAllTable];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
